﻿using System;
using BlueDot.Mobile.Core;
using RedirectWorkflowCallBack.Controllers;

namespace RedirectWorkflowCallBack.Views
{
    public partial class LoginView : ViewBase
    {
        #region Fields

        private const string LoginViewShown = WellKnownActions.ViewShown + "/LoginView";
        private const string LoginViewShowing = WellKnownActions.ViewShowing + "/LoginView";

        #endregion

        #region Constructors

        public LoginView()
        {
            InitializeComponent();
        }

        [DependencyConstructor]
        public LoginView(IMobileApplication application)
            : base(application)
        {
            InitializeComponent();
        }

        #endregion

        #region public

        [DependencyProperty]
        public IAuthenticationController Controller { get; set; }

        /// <summary>
        /// This action receiver is called when LoginView is shown.
        /// </summary>
        [Receiver(LoginViewShown)]
        public void OnLoginViewShown(object sender, EventArgs e)
        {

        }

        /// <summary>
        /// This action receiver is called when LoginView is showing.
        /// </summary>
        [Receiver(LoginViewShowing)]
        public void OnLoginViewShowing(object sender, EventArgs e)
        {
            // Replace the main menu on the shell with our view's menu
            ((Shell)Application.Shell).Menu = mainMenu;

            userNameTextBox.Text = string.Empty;
            passwordTextBox.Text = string.Empty;

            if (Application.Shell.ActiveView != this)
                errorText.Text = string.Empty;

        }

        public bool OnLogin()
        {
            bool success = false;

            try
            {
                success = Controller.AuthenticateUser(userNameTextBox.Text, passwordTextBox.Text);
            }
            catch
            {
                success = false;
            }

            if (!success)
                errorText.Text = "Login Failed!";

            return success;
        }

        #endregion
    }
}